package com.soundShape.mediators.photos
{
	
	import com.soundShape.events.SoundShapeEvent;
	import com.soundShape.itemRenderers.Thumbnail;
	import com.soundShape.mediators.AbstractViewMediator;
	import com.soundShape.popUps.PhotoInfo;
	import com.soundShape.popUps.SelectTreatment;
	import com.soundShape.utils.StringUtils;
	import com.soundShape.views.photos.Photos;
	import com.soundShape.vo.CustomerVO;
	import com.soundShape.vo.TreatmentVO;
	
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.ComboBox;
	import mx.controls.List;
	import mx.events.ListEvent;
	import mx.filters.IBitmapFilter;
	import mx.managers.PopUpManager;
	
	import spark.effects.AnimateFilter;
	import spark.effects.animation.MotionPath;
	import spark.effects.animation.RepeatBehavior;
	import spark.effects.animation.SimpleMotionPath;
	import spark.filters.BlurFilter;
	
	
	
	
	public class PhotosMediator extends AbstractViewMediator
	{
		
		
		
				
				public function PhotosMediator()
				{
					super(Photos);
				}
				
				
				/**
				 * Since the AbstractViewMediator sets the view via Autowiring in Swiz,
				 * we need to create a local getter to access the underlying, expected view
				 * class type.
				 */
				public function get view():Photos
				{
					return this._view as Photos;
				}
				
				/**
				 * The <code>init()</code> method is fired off automatically by the 
				 * AbstractViewMediator when the creation complete event fires for the
				 * corresponding ViewMediator's view. This allows us to listen for events
				 * and set data bindings on the view with the confidence that our view
				 * and all of it's child views have been created and live on the stage.
				 */
				override protected function init():void
				{
					super.init();
					
					this.setViews();
					
				}
				
				override protected function setViewDataBindings():void
				{
					super.setViewDataBindings();
					
					BindingUtils.bindProperty(this.view.cmbxCustomers,"dataProvider",this.applicationModel.customers,"source");
					setCustomerComboBox();
					
					BindingUtils.bindSetter(changeTreatmentVO,this.applicationModel,"treatmentVO");
					
					
				}	
				
				
				
				
				override protected function setViewListeners():void
				{
					super.setViewListeners();
					
					this.view.btnMenu.addEventListener(MouseEvent.CLICK, handleBtnMenu);
					
					this.view.cmbxCustomers.addEventListener(ListEvent.CHANGE, handleCmbxCustomersChange);
					
					this.view.btnGetFiles.addEventListener(MouseEvent.CLICK, handleBtnGetFiles);
					
					this.view.lstImg.addEventListener(SoundShapeEvent.IMAGE_SELECTED, handleImageSelected);
					
					this.view.imgRefresh.addEventListener(MouseEvent.CLICK, handleImgRefreshClick);
				}
				
				private function setViews():void
				{
					this.view.txtAreaCurrentTreatment.text = "";
					this.view.bcGreen.visible = false;
					doBlurSample();
					this.view.imgRefresh.enabled = false;
				}
				
				
				/**
				 * *******************************************************************************
				 * Handlers
				 * *******************************************************************************
				 */
				private function handleBtnMenu(mouseEvent:MouseEvent):void
				{
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.SWITCH_TO_MENU_VIEW));
				}
				
				private function handleCmbxCustomersChange(listEvent:ListEvent):void
				{
					this.applicationModel.customerVO = ComboBox(listEvent.currentTarget).selectedItem as CustomerVO;
					
					//TODO - clear existing phots
					clearPhotoTile();
					
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.GET_TREATMENTS_PHOTO);
					soundShapeEvent.data = this.applicationModel.customerVO.id;
					
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				
				
				public function handleBtnGetFiles(mouseEvent:MouseEvent):void
				{
					clearPhotoTile();
					
					this.dispatcher.dispatchEvent(new SoundShapeEvent(SoundShapeEvent.OPEN_FILE_BROWSER));
					
					this.view.bcGreen.visible = false;
				}
				
				protected function handleImageSelected(soundShapeEvent:SoundShapeEvent):void
				{
					var photoInfo:PhotoInfo = new PhotoInfo();
					photoInfo.photoDataVO = soundShapeEvent.data;
					photoInfo.applicationModel = this.applicationModel;
					PopUpManager.addPopUp(photoInfo,this.view,true);
					PopUpManager.centerPopUp(photoInfo);
				}
				
				private function changeTreatmentVO(treatmentVO:TreatmentVO):void
				{
					if(treatmentVO == null)
							return;
					
					this.view.txtAreaCurrentTreatment.text = "";
					
					var treatmentData:String = "name: " + treatmentVO.customer + "\n" +
											   "date: " + StringUtils.doDateParse(treatmentVO.treatmentDate) + "\n" +
											   "time: " + StringUtils.doTimeParse(treatmentVO.treatmentDate) + "\n" +
											   "comments: " + treatmentVO.comments;	
					
					this.view.txtAreaCurrentTreatment.text = treatmentData;
					
					this.view.btnGetFiles.enabled = true;
					this.view.bcGreen.visible = true;
				}
				
				private function handleImgRefreshClick(mouseEvent:MouseEvent):void
				{
					this.view.imgRefresh.enabled = false;
					var soundShapeEvent:SoundShapeEvent = new SoundShapeEvent(SoundShapeEvent.GET_TREATMENTS_PHOTO);
					soundShapeEvent.data = this.applicationModel.customerVO.id;				
					this.dispatcher.dispatchEvent(soundShapeEvent);
				}
				
				
				/**
				 * *******************************************************************************
				 * Swiz EventHanders
				 * *******************************************************************************
				 */
				[EventHandler(event="SoundShapeEvent.SET_TREATMENTS_PHOTO")]
				public function setTreatmentsPhoto(soundShapeEvent:SoundShapeEvent):void
				{
					var selectTreatment:SelectTreatment = new SelectTreatment();
					selectTreatment.treatments = soundShapeEvent.data;
					selectTreatment.applicationModel = this.applicationModel;
					PopUpManager.addPopUp(selectTreatment,this.view,true);
					PopUpManager.centerPopUp(selectTreatment);
					
					this.view.imgRefresh.enabled = true;
				}
				
				[EventHandler(event="SoundShapeEvent.IMAGES_READY")]
				public function setImages(soundShapeEvent:SoundShapeEvent):void
				{
					this.view.swfLdrProgress.visible = false;
					this.view.lstImg.dataProvider = new ArrayCollection(this.applicationModel.arrThumbsImageResources);
				}
				
				
				/**
				 * *******************************************************************************
				 * util
				 * *******************************************************************************
				 */
				private function setCustomerComboBox():void
				{
					this.view.cmbxCustomers.selectedIndex = -1;
					
					/*if(this.applicationModel.customerVO != null)
					{
						this.view.cmbxCustomers.selectedIndex = (this.view.cmbxCustomers.dataProvider as ArrayCollection).getItemIndex(this.applicationModel.customerVO);
					}
					else
					{
						this.view.cmbxCustomers.selectedIndex = -1;
					}*/
				}
				
				private var blurFilter:BlurFilter
				private var blurAnim:AnimateFilter;
				private var smpX:SimpleMotionPath;
				private var smpY:SimpleMotionPath;
				
				private function doBlurSample():void 
				{
					blurFilter = new BlurFilter();
					blurAnim = new AnimateFilter(this.view.bcGreen, blurFilter);
					
					smpX = new SimpleMotionPath("blurX",0,40);
					smpY = new SimpleMotionPath("blurY",0,40);
					blurAnim.motionPaths = Vector.<MotionPath>([smpX,smpY]);
					
					blurAnim.repeatCount = 0;
					blurAnim.repeatBehavior = RepeatBehavior.REVERSE;
					blurAnim.play();
				}
				
				
				private function stopAnimation():void 
				{
					blurAnim.stop();
				}
				
				/**
				 * not sure why, but the green checkmarks seem to persist on the images
				 * maybe because flex is recycling the itemrenders?  In any event, this
				 * function goes through and makes the green checkmarks invisible (default state)
				 */ 
				public function clearPhotoTile():void
				{
					if(this.view == null)
						return;
					
					if(this.view.lstImg.dataProvider == null)
							return;
					
					try
					{
						for(var n:int = 0; n<this.view.lstImg.dataProvider.length; n++)
						{
							var thumbNail:Thumbnail = Thumbnail(this.view.lstImg.dataGroup.getElementAt(n));
							thumbNail.resetChkMrkGreen();
						}
					}catch(e:Error){
						}
					
					ArrayCollection(this.view.lstImg.dataProvider).removeAll();
					ArrayCollection(this.view.lstImg.dataProvider).refresh();
					
				}
				
				
			
		}
		
	}
